package com.echat.companydata.tool;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by wangyitao on 2015/9/17.
 */
public class DateFormatUtil {

    public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";

    public static final String yyyyMMdd = "yyyy-MM-dd";

    public static final String yyyyMM = "yyyy-MM";

    public static final String yyyy = "yyyy";

    public static final String HHmmss = "HH:mm:ss";

    public static final String HHmm = "HH:mm";

    public static final String yyyyMMddHHmmssNOsplit = "yyyyMMddHHmmss";

    public static final String yyyyMMdd_E = "yyyy-MM-dd(E)";

    public static final DateFormat yyyyFormat = new SimpleDateFormat(yyyy);

    public static final DateFormat yyyyMMFormat = new SimpleDateFormat(yyyyMM);

    public static final DateFormat yyyyMMddFormat = new SimpleDateFormat(yyyyMMdd);

    public static final DateFormat yyyyMMddHHmmssFormat = new SimpleDateFormat(yyyyMMddHHmmss);

    public static final DateFormat yyyyMMddHHmmssNOsplitFormat = new SimpleDateFormat(yyyyMMddHHmmssNOsplit);

    public static final DateFormat HHmmFormat = new SimpleDateFormat(HHmm);

    private static final Map<String, DateFormat> dateFormatMap = new HashMap<>();

    private static final Map<String, Map<String, DateFormat>> dateFormat_EMap = new HashMap<>();

    static {
        dateFormatMap.put(yyyyMMdd, yyyyMMddFormat);
        dateFormatMap.put(yyyyMMddHHmmss, yyyyMMddHHmmssFormat);
        dateFormatMap.put(yyyyMMddHHmmssNOsplit, yyyyMMddHHmmssNOsplitFormat);
        dateFormatMap.put(HHmm, HHmmFormat);
    }

    public static final int error = -1;

    public static int getStartHour(long timestamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(timestamp));
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        calendar.clear();
        return hour;
    }

    /**
     * 根据格式获取DateFormat实例
     *
     * @param pattern
     * @return
     */
    private static DateFormat getDateFormat(String pattern) {
        DateFormat dateFormat = dateFormatMap.get(pattern);
        if (dateFormat == null) {
            try {
                dateFormat = new SimpleDateFormat(pattern);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            dateFormatMap.put(pattern, dateFormat);
            return dateFormat;
        } else {
            return dateFormat;
        }
    }


    public static Date parse(String date, String pattern) throws ParseException {
        if (date == null || StringUtil.isEmpty(pattern)) {
            return null;
        }
        DateFormat sdf = getDateFormat(pattern);
        if (sdf == null) {
            return null;
        }
        return sdf.parse(date);
    }


    public static String format(Date date, String pattern) {
        if (date == null || StringUtil.isEmpty(pattern)) {
            return null;
        }
        DateFormat sdf = getDateFormat(pattern);
        if (sdf == null) {
            return null;
        }
        return sdf.format(date);
    }

    public static String getTodayyyyy(Date date) {
        return yyyyFormat.format(date);
    }

    public static String getTodayyyyyMM(Date date) {
        return yyyyMMFormat.format(date);
    }

    public static String getTodayyyyyMMdd() {
        return yyyyMMddFormat.format(new Date());
    }

    public static String getTodayyyyMMddHHmmss() {
        return yyyyMMddHHmmssFormat.format(new Date());
    }

    public static String getTodayyyyMMddHHmmss(Date date) {
        return yyyyMMddHHmmssFormat.format(date);
    }

    /**
     * 获取两个日期相差天数
     *
     * @param startDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    public static int daysBetween(String startDate, String endDate) {
        Date startdate;
        Date enddate;
        try {
            startdate = yyyyMMddHHmmssFormat.parse(startDate);
            enddate = yyyyMMddHHmmssFormat.parse(endDate);
        } catch (ParseException e) {
            try {
                startdate = yyyyMMddFormat.parse(startDate);
                enddate = yyyyMMddFormat.parse(endDate);
            } catch (ParseException e1) {
                return error;
            }
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(startdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(enddate);
        long time2 = cal.getTimeInMillis();
        long betweenDays = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(betweenDays));
    }

    /**
     * 获取两个日期之间的所有日期
     *
     * @param startDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    public static List<String> dateBetween(Date startDate, Date endDate) {
        List<String> list = new ArrayList<String>(); //保存日期集合
        try {
            Date date = startDate;
            Calendar cd = Calendar.getInstance();//用Calendar 进行日期比较判断
            while (date.getTime() <= endDate.getTime()) {
                list.add(yyyyMMddFormat.format(date));
                cd.setTime(date);
                cd.add(Calendar.DATE, 1);//增加一天 放入集合
                date = cd.getTime();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 两个时间相差的分钟数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static int minutesBetween(String startDate, String endDate) {
        Date startdate;
        Date enddate;
        try {
            startdate = HHmmFormat.parse(startDate);
            enddate = HHmmFormat.parse(endDate);
        } catch (ParseException e) {
            return error;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(startdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(enddate);
        long time2 = cal.getTimeInMillis();
        long betweenDays = (time2 - time1) / (1000 * 60);
        return Integer.parseInt(String.valueOf(betweenDays));
    }

    /**
     * 两个时间相差的秒数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static int secondsBetween(Date startDate, Date endDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(endDate);
        long time2 = cal.getTimeInMillis();
        long res = (time2 - time1) / 1000;
        return Integer.parseInt(String.valueOf(res));
    }

    /**
     * 获取两个日期相差天数,日期参数不带时间
     *
     * @param startDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    public static int dateDiff(java.sql.Date startDate, java.sql.Date endDate) {
        if (startDate == null || endDate == null) {
            return 0;
        }
        long betweenDays = (endDate.getTime() - startDate.getTime()) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(betweenDays));
    }

    /**
     * @param date
     * @return 判断此事件是否是今天
     */
    public static boolean isToday(Date date) {
        if (date == null) {
            return false;
        }
        return yyyyMMddFormat.format(date).equals(yyyyMMddFormat.format(new Date()));
    }

    public static String getYyyyMMdd(Date date) {
        return yyyyMMddFormat.format(date);
    }

    public static String getYyyyMMddHHmmssNoSplit(Date date) {
        return yyyyMMddHHmmssNOsplitFormat.format(date);
    }

    /**
     * 获取日期为星期几，返回值为Calendar对象的常量，比如：Calendar.SATURDAY
     *
     * @param date
     * @return
     */
    public static int getDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 比较time1是否大于time2
     *
     * @param time1
     * @param time2
     * @param pattern
     * @return
     */
    public static boolean isGreaterThan(String time1, String time2, String pattern) {
        if (StringUtil.isEmpty(time1) || StringUtil.isEmpty(time2)) {
            return false;
        }
        Date date1 = null;
        Date date2 = null;
        try {
            date1 = parse(time1, pattern);
            date2 = parse(time2, pattern);
        } catch (ParseException e) {
            return false;
        }
        if (date1 == null || date2 == null) {
            return false;
        }
        return date1.getTime() > date2.getTime();
    }

    /**
     * 获取时间换算成分钟数：小时*60+分钟数
     *
     * @param date
     * @return
     */
    public static int getMinuteOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int min = calendar.get(Calendar.MINUTE);
        return hour * 60 + min;
    }

    public static long getTodayTimestamp() {
        try {
            return parse(format(new Date(), yyyyMMdd), yyyyMMdd).getTime();
        } catch (Exception e) {
            return 0L;
        }
    }

    public static long getTimestamp(String date, String format) {
        try {
            return parse(date, format).getTime();
        } catch (Exception e) {
            return 0L;
        }
    }

    public static void main(String[] arg) throws Exception {
        /*//YichatI18nUtil.init("F:\\javamaster\\chat\\target\\chat-1.0\\WEB-INF\\config\\languageChange.xml");
        Map<String, String> map = new HashMap<>();
        List<ConvertInfo> convertInfoList = SimpleXmlParser.parseXmlToBeanlist("F:\\javamaster\\chat\\target\\chat-1.0\\WEB-INF\\config\\languageChange.xml", ConvertInfo.class, "convertInfo");
        for (ConvertInfo info : convertInfoList) {
            map.put(info.getOriginal().toLowerCase(), info.getChangeTo());
        }
        for (Map.Entry<String, String> entry : map.entrySet()) {
            Locale l = new Locale(entry.getValue());
            System.out.println(l.getLanguage());
        }
        System.out.println(format(parse("2018-01-20", yyyyMMdd), yyyyMMdd_E, "5454"));*/
        //System.out.println(getMinuteOfDay(new Date()));
        Date d2 = parse("2017-12-03 00:00:00", yyyyMMddHHmmss);
        Date d1 = new Date();
        List<String> list = dateBetween(d2, d1);
        for (String s : list) {
            System.out.println(s);
        }
    }

}
